package core
{
	import events.StageEvent;
	
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	[Event(name="resize", type="capricornus.events.StageEvent")]
	public class StageManager extends EventDispatcher
	{
		private static var _ins:StageManager = null;
		
		private var _root:Sprite;
		
		private var _stage:Stage;
		
		public function StageManager()
		{
			init();
		}
		
		public static function get stage():Stage
		{
			return instance.stage;
		}
		
		public static function get stageWidth():int
		{
			return instance.stage.stageWidth;
		}
		
		public static function get stageHeight():int
		{
			return instance.stage.stageHeight;
		}
		
		public static function get instance():StageManager
		{
			if( null==_ins )
			{
				_ins = new StageManager();
			}
			
			return _ins;
		}
		
		private function init():void
		{
			_ins = this;
		}
		
		public function initialize( stage:Stage ):void
		{
			_stage = stage;
			_stage.align = StageAlign.TOP_LEFT;
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			_stage.addEventListener( Event.RESIZE, onStageResize, false, 0, true );
			_stage.addEventListener( Event.ACTIVATE, onStageActivate, false, 0, true );
			_stage.addEventListener( Event.DEACTIVATE, onStageDeActivate, false, 0, true );
		}
		
		public function get root():Sprite
		{
			return _root;
		}
		
		public function set root( value:Sprite ):void
		{
			_root = value;
		}
		
		public function get stage():Stage
		{
			return _stage;
		}
		
		public function set stage( value:Stage ):void
		{
			_stage = value;
		}
		
		private function onStageResize( e:Event ):void
		{
			dispatchEvent( new StageEvent( StageEvent.RESIZE ) );
		}
		
		private function onStageActivate( e:Event ):void
		{
			dispatchEvent( new StageEvent( StageEvent.ACTIVATE ) );
		}
		
		private function onStageDeActivate( e:Event ):void
		{
			dispatchEvent( new StageEvent( StageEvent.DEACTIVATE ) );
		}
	}
}